package com.abiao.shop.dao;

import com.abiao.shop.pojo.DiscountOrder;
import com.abiao.shop.pojo.Order;
import com.abiao.shop.pojo.User;
import com.abiao.shop.vojo.OrderVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderDao {

    /**
     * 获取用户对某个商品的秒杀订单
     * @param id
     * @param goodsId
     * @return
     */
    @Select("select * from discount_order where user_id = #{userId} and goods_id = #{goodsId}")
    DiscountOrder getOrderByGoodsIdUserId(@Param("userId") long id, @Param("goodsId") long goodsId);

    /**
     * 插入普通订单
     * @param order
     * @return
     */
    @Insert("insert into order_info (user_id,goods_id,goods_name,goods_count,goods_price,order_channel,status,create_date)values(" +
            "#{userId},#{goodsId},#{goodsName},#{goodsCount},#{goodsPrice},#{orderChannel},#{status},#{createDate})")
    //获取插入数据的id
    @SelectKey(keyColumn = "id",keyProperty = "id",resultType = long.class,before = false,statement = "select last_insert_id()")
    long insertOrder(Order order);

    /**
     * 插入秒杀订单
     * @param discountOrder
     * @return
     */
    @Insert("insert into discount_order (user_id,order_id,goods_id) values (#{userId},#{orderId},#{goodsId})")
    long insertDiscountOrder(DiscountOrder discountOrder);

    /**
     * 获取用户所有订单
     * @param user
     * @return
     */
    @Select("select o.*,g.goods_img from order_info o left join goods g on o.goods_id = g.id where user_id = #{id}")
    List<OrderVo> getUserAllOrders(User user);

    /**
     * 更新支付状态
     * @param user
     * @param orderId
     * @return
     */
    @Update("update order_info set status = 1 where id = #{orderId}")
    void payOrder(long orderId);
}
